﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/11 20:42:13
 * ************************************************************/
namespace Mozlite.FileStorages
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using Mozlite.Cryptography;
    using System.Web;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 文件类型。
    /// </summary>
    public class UserFile
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.FileStorages.UserFile"/>。
        /// </summary>
        public UserFile() { }

        /// <summary>
        /// 初始化类<see cref="T:Mozlite.FileStorages.UserFile"/>。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        public UserFile(IDataReader reader)
        {
            //this.SettingsID = reader.GetInt32("SettingsID");
            this.UserID = reader.GetInt32("UserID");
            this.FileID = reader.GetInt32("FileID");
            this.FolderID = reader.GetInt32("FolderID");
            this.FileName = reader.GetString("FileName");
            this.Extension = reader.GetString("Extension");
            this.DiskFileID = reader.GetString("DiskFileID");
            this.ContentType = reader.GetString("ContentType");
            this.ContentLength = reader.GetInt64("ContentLength");
            this.RefererCount = reader.GetInt32("RefererCount");
            this.CreateDate = reader.GetDateTime("CreateDate");
            this.LastModified = reader.GetDateTime("LastModified");
            this.FileType = reader.TGet<FileType>("FileType");
        }

        /// <summary>
        /// 初始化类<see cref="T:Mozlite.FileStorages.UserFile"/>。
        /// </summary>
        /// <param name="file">上传文件实例对象。</param>
        /// <param name="folderID">文件夹ID。</param>
        public UserFile(HttpPostedFileBase file, int folderID)
        {
            this.CreateDate = DateTime.Now;
            this.LastModified = DateTime.Now;
            //this.SettingsID = SiteSettings.Current.SettingsID;
            this.UserID = AppContext.Current.User.UserID;
            this.SetFileName(file.FileName);
            this.ContentLength = file.ContentLength;
            this.ContentType = file.ContentType;
            this.FolderID = folderID;
            this.file = file;
        }

        private HttpPostedFileBase file;

        /// <summary>
        /// 获取或设置文件夹创建日期。
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// 获取或设置最后修改日期。
        /// </summary>
        public DateTime LastModified { get; set; }

        /// <summary>
        /// 获取或设置网站设置ID。
        /// </summary>
        public int UserID { get; set; }

        ///// <summary>
        ///// 获取或设置网站设置ID。
        ///// </summary>
        //public int SettingsID { get; set; }

        /// <summary>
        /// 获取或设置文件的ID。
        /// </summary>
        public int FileID { get; set; }

        /// <summary>
        /// 获取或设置文件MD5值。
        /// </summary>
        public string DiskFileID { get; set; }

        /// <summary>
        /// 获取或设置文件名称。
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 获取或设置文件扩展名。
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// 获取或设置文档类型。
        /// </summary>
        public string ContentType { get; set; }

        /// <summary>
        /// 获取或设置文档大小。
        /// </summary>
        public long ContentLength { get; set; }

        /// <summary>
        /// 获取或设置引用的次数。
        /// </summary>
        public int RefererCount { get; set; }

        /// <summary>
        /// 打开文件。
        /// </summary>
        /// <returns>返回文件流。</returns>
        public FileStream OpenStream()
        {
            return System.IO.File.Open(this.Path, FileMode.Open, FileAccess.Read);
        }

        /// <summary>
        /// 获取文件的物理路径。
        /// </summary>
        public string Path
        {
            get
            {
                return Globals.GetUserFilePath("storages", DiskFileID[0], DiskFileID[1], DiskFileID[2], DiskFileID[3], DiskFileID[4], DiskFileID[5], DiskFileID[6], DiskFileID + ".config");
            }
        }

        /// <summary>
        /// 将临时文件移到存储文件中。
        /// </summary>
        /// <param name="tempFileName">临时文件名。</param>
        /// <returns>如果移动成功返回<c>true</c>，否则返回<c>false</c>。</returns>
        public bool Completed(string tempFileName = null)
        {
            try
            {
                if (tempFileName == null)
                    tempFileName = this.tempFileName;
                if (tempFileName == null)
                    return false;
                Globals.CreateDirectory(System.IO.Path.GetDirectoryName(Path));
                if (!File.Exists(this.Path))
                    File.Move(tempFileName, this.Path);
                return true;
            }
            catch { }
            return false;
        }

        /// <summary>
        /// 获取文件夹ID。
        /// </summary>
        public int FolderID { get; set; }

        /// <summary>
        /// 获取文件名加上扩展名。
        /// </summary>
        public string FullName
        {
            get
            {
                return this.FileName + "." + this.Extension;
            }
        }

        /// <summary>
        /// 设置文件名。
        /// </summary>
        /// <param name="fileName">文件名，包含扩展名。</param>
        public void SetFileName(string fileName)
        {
            this.FileName = System.IO.Path.GetFileNameWithoutExtension(fileName);
            this.Extension = Globals.GetExtension(fileName);
            this.FileType = MimeTypes.GetFileType(this.Extension);
        }

        private string tempFileName;
        /// <summary>
        /// 上传文件。
        /// </summary>
        public void SaveAsTemp()
        {
            if (file == null)
                return;
            tempFileName = this.file.SaveAsTemp();
            this.DiskFileID = Cryptors.GetHex(Cryptors.BatchMake(tempFileName, EncryptionAlgorithm.MD5));
        }

        /// <summary>
        /// 获取或设置文件类型。
        /// </summary>
        public FileType FileType { get; set; }
    }
}